﻿/*
 * @author: wizardc
 */

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Dou.UI
{
    /// <summary>
    /// 按钮
    /// </summary>
    public class DButton : Button
    {
        /// <summary>
        /// 是否启用安全点击，启用后在点击后的指定时间内的再次点击无效
        /// </summary>
        public static bool useSafeClick = false;
        
        /// <summary>
        /// 安全点击检测间隔
        /// </summary>
        public static float safeClickInterval = 0.3f;

        /// <summary>
        /// 是否启用长按检测
        /// </summary>
        public bool useLongPress = false;

        /// <summary>
        /// 按下时间超过该值进入长按模式
        /// </summary>
        public float longPressTime = 0.5f;

        /// <summary>
        /// 长按事件发送间隔
        /// </summary>
        public float longPressInterval = 0.3f;
        
        [SerializeField]
        private bool _useGray = false;
        [SerializeField]
        private ButtonClickedEvent _onSafeClick = new ButtonClickedEvent();
        [SerializeField]
        private ButtonClickedEvent _onLongPress = new ButtonClickedEvent();

        private float _lastClickTime = 0;
        
        private bool _isPressed = false;
        private float _longPressTime = 0;

        /// <summary>
        /// 是否置灰
        /// </summary>
        public bool useGray
        {
            set
            {
                if (_useGray != value)
                {
                    _useGray = value;
                    UIUtil.SetGray(transform, _useGray);
                }
            }
            get => _useGray;
        }

        /// <summary>
        /// 安全点击事件
        /// </summary>
        public ButtonClickedEvent onSafeClick
        {
            set => _onSafeClick = value;
            get => _onSafeClick;
        }

        /// <summary>
        /// 长按事件
        /// </summary>
        public ButtonClickedEvent onLongPress
        {
            set => _onLongPress = value;
            get => _onLongPress;
        }

        protected override void Awake()
        {
            base.Awake();
            onClick.AddListener(ClickHandler);
        }

        private void ClickHandler()
        {
            if (useSafeClick)
            {
                var now = Time.timeScale;
                if (now > _lastClickTime + safeClickInterval)
                {
                    _lastClickTime = now;
                    _onSafeClick.Invoke();
                }
            }
            else
            {
                _onSafeClick.Invoke();
            }
        }

        public override void OnPointerDown(PointerEventData eventData)
        {
            base.OnPointerDown(eventData);
            if (useLongPress)
            {
                _isPressed = true;
                _longPressTime = Time.timeScale + longPressTime;
            }
        }

        public override void OnPointerUp(PointerEventData eventData)
        {
            base.OnPointerUp(eventData);
            _isPressed = false;
        }

        private void LateUpdate()
        {
            if (Application.isFocused && _isPressed)
            {
                var now = Time.timeScale;
                if (now > _longPressTime)
                {
                    _longPressTime += longPressInterval;
                    _onLongPress.Invoke();
                }
            }
        }
    }
}
